from django.db import models

from apps.booking.models import Appointment
# from apps.booking.models import Appointment
from apps.core.models import GenericModel
from pgcrypto import fields
from apps.users.models import Provider
from apps.client.models import Client
from django.contrib.postgres.fields import ArrayField
from apps.document.types import DOCUMENT_TYPE, DocumentType, NOTES_TYPE, NoteType
import uuid


# Create your models here.


def upload_other_documents(instance, filename):
    path = "other/documents/{document_id}/{filename}".format(
        document_id=str(instance.document_id), filename=filename
    )
    return path


def upload_appointment_notes_documents(instance, filename):
    path = "other/appointment/notes/{document_id}/{filename}".format(
        document_id=instance.id, filename=filename
    )
    return path


def upload_client_documents(instance, filename):
    path = "other/client/documents/{document_id}/{filename}".format(
        document_id=instance.client.id, filename=filename
    )
    return path


class DocumentAndForms(GenericModel):
    title = fields.CharPGPSymmetricKeyField(max_length=255, null=True)
    type = models.CharField(max_length=3, choices=DOCUMENT_TYPE, default=DocumentType.PROGRESS_NOTES.value)
    is_checked = models.BooleanField(default=True)
    content = ArrayField(models.JSONField(), null=True, blank=True)

    class Meta:
        db_table = 'documents_and_form'


class AppointmentNotes(GenericModel):
    appointment = models.ForeignKey(Appointment, on_delete=models.CASCADE,
                                    null=True,
                                    related_name='appointment_notes')

    content = ArrayField(models.JSONField(), null=True, blank=True)
    title = fields.CharPGPSymmetricKeyField(max_length=255, null=True)
    type = models.CharField(max_length=2, choices=NOTES_TYPE, default=NoteType.APPOINTMENT_NOTE.value)
    client = models.ForeignKey(Client, on_delete=models.CASCADE, null=True)
    date = models.DateTimeField(null=True)
    time = models.TimeField(null=True)
    note = models.TextField(null=True)

    class Meta:
        db_table = 'appointment_notes'


class AppointmentNotesFiles(GenericModel):
    appointment_notes = models.ForeignKey(AppointmentNotes,
                                          on_delete=models.CASCADE,
                                          related_name='appointment_notes_files')
    file = models.FileField(upload_to=upload_appointment_notes_documents, max_length=255, blank=True, null=True)
    title = models.CharField(max_length=255, null=True, blank=True)

    class Meta:
        db_table = 'appointment_notes_files'


class ClientDocuments(GenericModel):
    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='client_documents')
    content = ArrayField(models.JSONField(), null=True, blank=True)
    title = models.TextField(null=True)
    file = models.FileField(upload_to=upload_client_documents, max_length=255, blank=True, null=True)
    updated_at = models.DateTimeField(null=True)

    class Meta:
        db_table = 'client_documents'


class OtherDocuments(GenericModel):
    document_id = models.UUIDField(default=uuid.uuid4, unique=True)
    document = models.FileField(upload_to=upload_other_documents, max_length=255, blank=True, null=True)
    document_name = models.TextField(null=True)
    title = models.TextField(null=True)

    class Meta:
        db_table = 'other_documents'
